package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.MapConstant;
import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.GiftCard;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.service.*;
import com.tbit.uqbike.client.util.MD5PasswordUtil;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 区域礼品卡
 *
 * @Author: chen
 * @Description: 区域礼品卡
 * @Date: 创建于 15:28 2020/1/3
 */
@Controller
@RequestMapping("/giftCard")
public class GiftCardController {
    @Autowired
    private GeoService geoService;
    @Autowired
    private GiftCardService giftCardService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private UserService userService;
    @Autowired
    private CouponService couponService;

    /**
     * 兑换
     *
     * @param token    令牌
     * @param cardNO   礼品卡编号
     * @param response 响应
     * @ignoreParams request
     */
    @RequestMapping("/exchange")
    public void exchange(String token, String cardNO, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            //Assert.notNull(adAccountId, "-10001", "品牌id不能为空");

            User user = tokenService.getUser(token);
            GiftCard giftCard = new GiftCard();
            giftCard.setUserId(user.getUserId());
            giftCard.setCardNO(cardNO);

            /**礼品卡兑换*/
            giftCardService.edit(giftCard, user);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    /**
     * 兑换
     *
     * @param cardNO   礼品卡编号
     * @param response 响应
     * @ignoreParams request
     */
    @RequestMapping("/exchangeApi")
    public void exchangeApi(String cardNO, Integer userId, HttpServletRequest request, HttpServletResponse response, String sign, String param, Long date) {
        Map<String, Object> map = new HashMap<>(8);
        boolean result = MD5PasswordUtil.signCheck(sign, date, param);
        if (!result) {
            throw new BaseException("-190002", "签名校验失败");
        }
        try {
            User user = userService.getByUserId(userId);
            GiftCard giftCard = new GiftCard();
            giftCard.setUserId(user.getUserId());
            giftCard.setCardNO(cardNO);

            /**礼品卡兑换*/
            giftCardService.edit(giftCard, user);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }


    /**
     * 查询用户礼品卡对应
     *
     * @param token       令牌
     * @param accountId   品牌id
     * @param lo          经度
     * @param la          纬度
     * @param mapType     地图类型
     * @param adAccountId 区域id
     * @param response    响应
     * @ignoreParams request
     */
    @RequestMapping("/query")
    public void query(String token, Integer accountId, Double lo, Double la, Integer mapType, Integer adAccountId, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            //Assert.notNull(adAccountId, "-10001", "品牌id不能为空");
            User user = tokenService.getUser(token);
            if (adAccountId == null) {
                if (mapType == null) {
                    mapType = MapConstant.MAP_GOOGLE;
                }
                Integer agentId = geoService.getBelongAccount(accountId, lo, la, mapType);
                if (agentId == null) {
                    throw new BaseException("-3050", "运营区域外,请在运营区域内使用");
                }
                adAccountId = agentId;
            }
            List<GiftCard> list = giftCardService.getByUserId(adAccountId, user.getUserId());
            list = formatGiftCard(list);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, list);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    /**
     * 格式化卡券列表
     * @param list 卡券列表
     * @return
     */
    private List<GiftCard> formatGiftCard(List<GiftCard> list){
        for (GiftCard giftCard : list) {
            giftCard.format(couponService);
        }
        return list;
    }
}
